package com.cloud.school.app.controller;

import com.cloud.school.app.config.security.SecurityUserUtils;
import com.cloud.school.app.model.AnswerModel;
import com.cloud.school.app.model.QuestionModel;
import com.cloud.school.app.service.AnswerService;
import com.cloud.school.app.service.QuestionService;
import com.cloud.school.app.util.Pager;
import com.cloud.school.app.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName Answer
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 1:03
 * @Version 1.0
 */
@Api(tags = "个人提问控制器")
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private AnswerService answerService;

    @ApiOperation("分页查询所有提问")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResponseUtil<Pager<QuestionModel>> all(@ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(questionService.selectAllDetail(pNo));
    }

    @ApiOperation("根据questionId查询question详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ResponseUtil<QuestionModel> detail(@ApiParam("提问Id") @RequestParam Long questionId) {
        return ResponseUtil.ok(questionService.selectDetailById(questionId));
    }

    @ApiOperation("根据questionId查询question详情(回答无层级关系)")
    @RequestMapping(value = "/detailWithoutAnswerFormat", method = RequestMethod.GET)
    public ResponseUtil<QuestionModel> detailWithAnswers(@ApiParam("提问Id") @RequestParam Long questionId) {
        return ResponseUtil.ok(questionService.selectDetailWithoutAnswerFormatById(questionId));
    }

    @ApiOperation("分页查询个人提问")
    @RequestMapping(value = "/queryByUid", method = RequestMethod.GET)
    public ResponseUtil<Pager<QuestionModel>> getByUid(@ApiParam("用户id") @RequestParam Long userId, @ApiParam("页数") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(questionService.selectDetailByUid(userId, pNo));
    }

    @ApiOperation("发布问题")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseUtil<Boolean> create(@RequestBody QuestionModel questionModel) {
        questionModel.setCreateUid(SecurityUserUtils.getCurrentUserId());
        return ResponseUtil.ok(questionService.save(questionModel));
    }

    @ApiOperation("回答问题")
    @RequestMapping(value = "/answer", method = RequestMethod.POST)
    public ResponseUtil<Boolean> answer(@RequestBody AnswerModel answerModel) {
        answerModel.setCreateUid(SecurityUserUtils.getCurrentUserId());
        return ResponseUtil.ok(answerService.save(answerModel));
    }

    @ApiOperation("删除提问")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseUtil<Boolean> deleteById(@ApiParam("提问Id") @RequestParam Long questionId) {
        return ResponseUtil.ok(questionService.deleteById(questionId));
    }
}
